import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class P06_Panel extends JPanel  implements MouseListener, MouseMotionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[5];
	
	public int man=0;
	
	public int position =0;
	
	private int[] face_check = new int[14]; 
	private List<Check_Position> check_position = new ArrayList<Check_Position>();
	
	private boolean moving=false;
	
	public P06_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/06.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		imageList[0]=new ImageIcon(getClass().getResource("images/6/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/6/check.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/6/check_.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/6/left_face.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/6/right_face.png")).getImage();
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		for(int i=0;i<face_check.length;i++){
			face_check[i]=0;
		}
		
		
		check_position.add(new Check_Position(246, 373));
		check_position.add(new Check_Position(246, 578));
		check_position.add(new Check_Position(246, 678));
		
		check_position.add(new Check_Position(858, 333));
		check_position.add(new Check_Position(858, 527));
		check_position.add(new Check_Position(858, 630));
		
		check_position.add(new Check_Position(1020, 331));
		check_position.add(new Check_Position(1020, 484));
		check_position.add(new Check_Position(1020, 579));
		check_position.add(new Check_Position(1020, 691));
		
		check_position.add(new Check_Position(1614, 388));
		check_position.add(new Check_Position(1614, 484));
		check_position.add(new Check_Position(1614, 579));
		check_position.add(new Check_Position(1614, 691));
		
		
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
		
		//left face
		g.drawImage(imageList[3], 310, 343, null);
		
		//right face
		g.drawImage(imageList[4], 1080, 343, null);
		
		//check boxes
		for(int i=0;i<face_check.length;i++){
			if(face_check[i]==0){
				g.drawImage(imageList[1], check_position.get(i).xpos, check_position.get(i).ypos, null);
			}else{
				g.drawImage(imageList[2], check_position.get(i).xpos, check_position.get(i).ypos, null);
			}
		}
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
		
		//check boxes
		//58,56
		for(int i=0;i<check_position.size();i++){
			if(check_position.get(i).xpos<=e.getX() && e.getX()<=check_position.get(i).xpos+58 
					&& check_position.get(i).ypos<=e.getY() && e.getY()<=check_position.get(i).ypos+56){
				if(face_check[i]==0){
					face_check[i]=1;
				}else{
					face_check[i]=0;
				}
			}
		}
		
				
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	if(menu_presseed[0] &&1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				
				for(int i=0;i<face_check.length;i++){
					listener.onChangeValue(7+i, face_check[i]);
				}
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
    	
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
    
    
}